package com.baofu.paycore.manager.storage;

import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.google.common.collect.Maps;
import com.baofu.paycore.dal.model.base.BaseDO;
import com.baofu.paycore.manager.storage.impl.*;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * Mapper 容器
 *
 * User: LZQ Date: 2016/04/20  Version: 1.0
 */
@Slf4j
@Component
public class StorageContainer {

    /**
     * 支付主单
     */
    @Autowired
    private PayManager payManager;

    /**
     * 支付资金单
     */
    @Autowired
    private SubPayManager subPayManager;

    /**
     * 充值/提现主单
     */
    @Autowired
    private ClearManager clearManager;

    /**
     * 充值/提现银行资金单
     */
    @Autowired
    private ClearBankManager clearBankManager;

    /**
     * 充值/提现账户资金单
     */
    @Autowired
    private ClearAccountManager clearAccountManager;

    /**
     * 转账主单
     */
    @Autowired
    private TransferManager transferManager;

    /**
     * 转账资金单
     */
    @Autowired
    private SubTransferManager subTransferManager;

    /**
     * 退款主单处理Manager
     */
    @Autowired
    private RefundManager refundManager;

    /**
     * 退款资金单处理Manager
     */
    @Autowired
    private SubRefundManager subRefundManager;

    /**
     * 预授权处理
     */
    @Autowired
    private PreAuthManager preAuthManager;

    /**
     * 预授权资金单处理
     */
    @Autowired
    private SubPreAuthManager subPreAuthManager;

    @Autowired
    private FreezeManager freezeManager;

    /**
     * 模型 getClass().getSimpleName()作为 key
     */
    private Map<String,Storage> map = Maps.newHashMap();

    @PostConstruct
    public void init(){
        // 支付单
        map.put("PayDO", payManager);
        map.put("SubPayDO", subPayManager);

        // 充值单
        map.put("ClearDO", clearManager);
        map.put("ClearAccountDO", clearAccountManager);
        map.put("ClearBankDO", clearBankManager);

        //转账
        map.put("TransferDO", transferManager);
        map.put("SubTransferDO", subTransferManager);

        //退款
        map.put("RefundDO", refundManager);
        map.put("SubRefundDO", subRefundManager);

        //预授权
        map.put("PreAuthDO", preAuthManager);
        map.put("SubPreAuthDO", subPreAuthManager);

        //冻结
        map.put("FreezeDO",freezeManager);
    }

    /**
     * 根据模型类型获取对应的存储器。
     *
     * @param t           模型
     * @return            存储器
     */
    public <T extends BaseDO> Storage getStore(T t) {
        Storage storage = map.get(t.getClass().getSimpleName());
        if(storage == null){
            log.error("不支持的存储方式：{}",t.getClass());
            throw new BizServiceException(PayCoreErrorCode.VALUE_NOT_SUPPORT, "不支持的存储方式:"+t.getClass());
        }
        return storage;
    }

}
